using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;
using Lizard.Remoting.Settings;

namespace Ltf.Remoting.Settings
{
	[Serializable]
	public class TFSettings : ISettings
	{
		private const string defaultMetaPath = "c:\\LizardMetaData";
		public TFSettings()
		{
			tempPath = Environment.GetEnvironmentVariable("Temp");
			includedDrives = "C";
			excludedfolders = "bin obj Debug Release x86 x64";
			excludedfiles = "exe dll ncb pdb suo";
			ltfMetaFolderName = defaultMetaPath;

			if (File.Exists("C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"))
				editTool = "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe";
			else if (File.Exists("C:\\Program Files\\Notepad++\\Notepad++.exe"))
				editTool = "C:\\Program Files\\Notepad++\\Notepad++.exe";
			else if (File.Exists(Path.Combine(Environment.SystemDirectory, "Notepad.exe")))
				editTool = Path.Combine(Environment.SystemDirectory, "Notepad.exe");

		}

		private string user;

		public string User
		{
			get { return user; }
			set { user = value; }
		}

		public string GetTFSUserName()
		{
			if (!string.IsNullOrEmpty(user))
				return user;
			else
				return Environment.UserDomainName + "\\" + Environment.UserName;
		}

		private string password;

		public string Password
		{
			get { return password; }
			set { password = value; }
		}

		private string domain;

		public string Domain
		{
			get { return domain; }
			set { domain = value; }
		}


		private string server;

		public string Server
		{
			get { return server; }
			set { server = value; }
		}

		private string editTool;

		public virtual string EditTool
		{
			get { return editTool; }
			set { editTool = value; }
		}




		private string tempPath;

		public virtual string TempPath
		{
			get { return tempPath; }
			set { tempPath = value; }
		}

		private string metaPath = defaultMetaPath;

		public virtual string MetaPath
		{
			get { return metaPath; }
			set { 
				metaPath = value;
				if (!Directory.Exists(MetaPath))
				{
					metaPath = defaultMetaPath;
				}
			}
		}
		
		private string defaultWorkingPath;

		public virtual string DefaultWorkingPath
		{
			get { return defaultWorkingPath; }
			set { defaultWorkingPath = value; }
		}


		private string x64InstallPath;

		public virtual string X64InstallPath
		{
			get { return x64InstallPath; }
			set { x64InstallPath = value; }
		}

		private string ltfMetaFolderName;

		public virtual string LtfMetaFolderName
		{
			get { return ltfMetaFolderName; }
			set {
				if (excludedfoldersList!=null && excludedfoldersList.Contains(ltfMetaFolderName))
					excludedfoldersList.Remove(ltfMetaFolderName);
				ltfMetaFolderName = value;
				if (excludedfoldersList != null && !excludedfoldersList.Contains(ltfMetaFolderName))
					excludedfoldersList.Add(ltfMetaFolderName);
			}
		}

		private string includedDrives;
		private List<string> includedDrivesList;

		public virtual string IncludedDrives
		{
			get { return includedDrives; }
			set
			{
				includedDrives = value;
				includedDrivesList = new List<string>(includedDrives.ToUpper().Split(' '));
				if (!includedDrivesList.Contains("$"))
					includedDrivesList.Add("$");
			}
		}

		private string excludedfolders;
		private List<string> excludedfoldersList;

		public virtual string Excludedfolders
		{
			get { return excludedfolders; }
			set
			{
				excludedfolders = value;
				excludedfoldersList = new List<string>(excludedfolders.ToUpper().Split(' '));
				excludedfoldersList.Add(ltfMetaFolderName);
			}
		}

		private string excludedfiles;
		private List<string> excludedfilesList;

		public virtual string Excludedfiles
		{
			get { return excludedfiles; }
			set
			{
				excludedfiles = value;
				excludedfilesList = new List<string>(excludedfiles.ToUpper().Split(' '));
			}
		}

		public virtual bool IsFilePathIncluded(string path)
		{
			if (!includedDrivesList.Contains(path.Substring(0, 1).ToUpper()))
				return false;
			if (excludedfilesList.Contains(Path.GetExtension(path).Trim().ToUpper()))
				return false;
			return true;
		}

		public virtual bool IsFolderPathIncluded(string path)
		{
			if (!includedDrivesList.Contains(path.Substring(0, 1).ToUpper()))
				return false;
			if (excludedfoldersList.Contains(Path.GetFileName(path).ToUpper().Trim()))
				return false;
			return true;
		}

		public virtual bool IsFullFilePathIncluded(string path)
		{
			if (!includedDrivesList.Contains(path.Substring(0, 1).ToUpper()))
				return false;
			string fName = Path.GetDirectoryName(path).Trim();
			if (excludedfoldersList.Contains(Path.GetFileName(fName).ToUpper()))
				return false;
			if (excludedfilesList.Contains(Path.GetExtension(path).ToUpper().Trim()))
				return false;
			foreach (string folder in (fName.Split('\\')))
				if (excludedfoldersList.Contains(folder.ToUpper()))
					return false;
			return true;
		}

		private bool autoCheckoutChangedFiles;

		public bool AutoCheckoutChangedFiles
		{
			get { return autoCheckoutChangedFiles; }
			set { autoCheckoutChangedFiles = value; }
		}

		private List<TFSecondarySettings>  alternativeServerSettings = new List<TFSecondarySettings>();

		public virtual List<TFSecondarySettings> AlternativeServerSettings
		{
			get {return alternativeServerSettings;}
			set { alternativeServerSettings = value; }
		}
	}


}
